package com.onelap.bls.tc.activity.index;

import android.bluetooth.BluetoothDevice;
import android.support.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.onelap.bls.tc.R;

import java.util.List;


public class DeviceListAdapter extends BaseQuickAdapter<DeviceBean, BaseViewHolder> {

    DeviceListAdapter(@Nullable List<DeviceBean> data) {
        super(R.layout.item_device_list, data);

    }

    @Override
    protected void convert(BaseViewHolder helper, DeviceBean item) {
        helper.setText(R.id.tv_name, item.getDevice().getName());
        helper.setText(R.id.tv_address, item.getDevice().getAddress());
        helper.setText(R.id.tv_paired, item.getDevice().getBondState() == BluetoothDevice.BOND_BONDED ? "Paired" : "");
        helper.setText(R.id.tv_rssi, item.getRssi() == 0 || item.getRssi() == -0 ? "" : "Rssi = " + String.valueOf(item.getRssi()));
        helper.setGone(R.id.tv_temp, false);
        if (item.getBroadcastPacket() != null) {
            helper.setText(R.id.tv_temp, getStrScanRecord(item.getBroadcastPacket()));
        }

        helper.setBackgroundColor(R.id.rl_root, item.isSelect() ? mContext.getResources().getColor(R.color.color5) : mContext.getResources().getColor(R.color.color4));
        helper.setTextColor(R.id.tv_name, item.isSelect() ? mContext.getResources().getColor(R.color.colorWhite) : mContext.getResources().getColor(R.color.BLS_black));
        helper.setTextColor(R.id.tv_address, item.isSelect() ? mContext.getResources().getColor(R.color.colorWhite) : mContext.getResources().getColor(R.color.color3));
        helper.setTextColor(R.id.tv_paired, item.isSelect() ? mContext.getResources().getColor(R.color.colorWhite) : mContext.getResources().getColor(R.color.color3));
        helper.setTextColor(R.id.tv_rssi, item.isSelect() ? mContext.getResources().getColor(R.color.colorWhite) : mContext.getResources().getColor(R.color.color3));
    }

    /**
     * 字符转化 bytesToHex
     */
    private static String getStrScanRecord(byte[] bytes) {
        char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }


}
